﻿using Lis.IBLL;
using Lis.Model;
using Lis.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lis.Common;

namespace Lis.BLL
{
    public class UserInfoService:BaseService<UserInfo>,IUserInfoService
    { 
        #region 01，根据卡号获取读者信息+ServiceResult<object> GetById(string Id)
        /// <summary>
        /// 根据卡号获取读者信息
        /// </summary>
        /// <param name="Id">读者卡号</param>
        /// <returns></returns>
        public ServiceResult<object> GetById(string Id)
        {
            if (!string.IsNullOrEmpty(Id))//判断是否为空
            {
                if (Id.CheckStr())//判断非法字符
                {
                    return new ServiceResult<object>()
                    {
                        Success = true,
                        Result = ((UserInfoRepository)DBSession.SysUserInfoRepository).GetById(Id),
                        Message = "",
                        Code = "10000"
                    };
                }
                else
                {
                    return new ServiceResult<object>()
                    {
                        Success = false,
                        Result = null,
                        Message = "请输入正确的卡号进行查询",
                        Code = "10002"
                    };
                }
            }
            else
            {
                return new ServiceResult<object>()
                {
                    Success = false,
                    Result = null,
                    Message = "请输入正确的卡号进行查询",
                    Code = "10001"
                };
            }
        } 
        #endregion  
    
        #region 02，分页查询+override IQueryable<UserInfo> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<UserInfo, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserInfo, S>> orderByLambda)
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="total">总条数</param>
        /// <param name="isAsc">是否倒序</param>
        /// <param name="whereLambda">查询lambda</param>
        /// <param name="orderByLambda">排序lambda</param>
        /// <returns></returns>
        public override IQueryable<UserInfo> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<UserInfo, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserInfo, S>> orderByLambda)
        {
            return DBSession.SysUserInfoRepository.GetPageEntities<S>(pageSize, pageIndex, out total, isAsc, whereLambda, orderByLambda);
        } 
        #endregion

        #region 03，新增+override bool Add(UserInfo entity)
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体模型</param>
        /// <returns></returns>
        public override bool Add(UserInfo entity)
        {
            //1,日志表里面插数据
            #region 1，在日志表里面插数据
            var log = new LogInfo()
            {
                DZTM = entity.DZTM,
                DCTM = string.Empty,
                CZY = CirculateConfig.Operator,
                CZRQ = DateTime.Now,
                CZLXDM = "XZ",
                SQLSTR = @"Insert into dzxxb(dztm,xm,mm,zjlx,xb,yj,bzrq,yxrq,dwmc,dzlx,yktyj) values ('"+entity.DZTM+"','"+entity.XM+"','"+entity.MM+"','"+entity.ZJLX+"','"+entity.XB+"',0.0,TO_DATE('"+entity.BZRQ.ToString()+"','YYYY-MM-DD'),TO_DATE('"+entity.YXRQ.ToString()+"','YYYY-MM-DD'),'"+entity.DWMC+"','"+entity.DZLX+"',0.0)",
                LYB = "zydck",
                MARC = "U",
                GCDWDM = "1"
            };
            DBSession.SysLogInfoRepository.Add(log);
            #endregion

            return DBSession.SysUserInfoRepository.Add(entity);
        } 
        #endregion

        #region 04，更新+override bool UpDate(UserInfo entity)
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public override bool UpDate(UserInfo entity)
        {
            //1,日志表里面插数据
            #region 1，在日志表里面插数据
            var log = new LogInfo()
            {
                DZTM = entity.DZTM,
                DCTM = string.Empty,
                CZY = CirculateConfig.Operator,
                CZRQ = DateTime.Now,
                CZLXDM = "GR",
                SQLSTR = @"update dzxxb set dztm='" + entity.DZTM + "',xm='" + entity.XM + "',mm='" + entity.MM + "',zp=null,zjlx='" + entity.ZJLX + "',zjbm=null,csn=null,xb='" + entity.XB + "',xl=null,zc=null,zy=null,txdz=null,yzbm=null,dhhm=null,dzyj=null,yjdw=null,ejdw=null,yj=0.0,bzrq=TO_DATE('" + entity.BZRQ.ToString() + "','YYYY-MM-DD'),yxrq=TO_DATE('" + entity.YXRQ.ToString() + "','YYYY-MM-DD'),dxrq=null,gsrq=null,dwmc='" + entity.DWMC + "',dzlx='" + entity.DZLX + "',yy=null,yktyj=0.0,yl1=null,yl2=null,ffczy=null,ffsj=null where (dztm = '" + entity.DZTM + "')",
                MARC = "U",
                GCDWDM = "1"
            };
            DBSession.SysLogInfoRepository.Add(log);
            #endregion
            return DBSession.SysUserInfoRepository.Update(entity);
        } 
        #endregion

        public override bool Delete(UserInfo entity)
        {
            throw new NotImplementedException();
        }

        #region 05，根据卡号获取读者信息+UserInfo GetById(string Id)
        /// <summary>
        /// 根据卡号获取读者信息
        /// </summary>
        /// <param name="Id">读者卡号</param>
        /// <returns></returns>
        public UserInfo GetEntityById(string Id)
        {
            return ((UserInfoRepository)DBSession.SysUserInfoRepository).GetById(Id);
        } 
        #endregion
    }
}
